package collections.map;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

public class HashMapDemo {

  public static void main(String[] args) {

    Country china = new Country("China", 1000l);
    Country jap = new Country("Jap", 1000l);
    Country usa = new Country("USA", 1000l);

    Map<Country, String> map = new HashMap<Country, String>();

    map.put(china, "china");
    map.put(jap, "tokyo");
    map.put(usa, "newyerk");


    System.out.println(map);

  }


  @Test
  public void test() {

    Country china = new Country("China", 1000l);
    Country jap = new Country("Jap", 1000l);
    Country usa = new Country("USA", 1000l);

    Map<Country, String> map = new HashMap<Country, String>();

    map.put(china, "china");
    map.put(jap, "tokyo");
    map.put(usa, "newyerk");


    System.out.println(map);
  }

  @Test
  public void test2(){
    Map<String, String> map = new HashMap<String, String>();

    map.put("panda", "china");
    map.put("jap", "tokyo");
    map.put("usa", "newyerk");

//    System.out.println(map.putIfAbsent("tom","eng"));
//    System.out.println(map.get("tom"));
//
//    System.out.println(map.putIfAbsent("panda","sichuan"));
//    System.out.println(map.get("panda"));


    System.out.println(map.computeIfAbsent("tom", new Function<String, String>() {
      @Override
      public String apply(String s) {
        return "eng";
      }
    }));
    System.out.println(map.get("tom"));

    System.out.println(map.computeIfAbsent("panda", new Function<String, String>() {
      @Override
      public String apply(String s) {
        return "sichuan";
      }
    }));
    System.out.println(map.get("panda"));

  }

}

